/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logic;

/**
 *
 * @author phong
 */
public abstract class Type {

    public final static Type UNKNOWN_TYPE = new UnknownType();
    public final static Type TRUTH_TYPE = new TruthValueType();
    public final static Type ENTITY_TYPE = new EntityType();
    

    public abstract boolean matches(Type other);

    public abstract Type resolve(Type other);

    public abstract String getName();

    @Override
    public int hashCode() {
        return getName().hashCode();
    }

    public static Type parseType(String input)
    {
        input = input.replaceAll(" ", "");

        if (input.charAt(0) == '<') {
            assert input.charAt(input.length()-1) == '>';
            int paren = 0;
            int i = 0;

            for ( ; i < input.length(); i++) {
                char c = input.charAt(i);

                if (c == '<')
                    paren++;
                else if (c == '>') {
                    paren--;
                    assert paren >= 0;
                }
                else if (c == ',') {
                    if (paren == 1) break;
                }
            }
            return new ComplexType(
                    parseType(input.substring(1, i)),
                    parseType(input.substring(i+1, input.length()-1)));
        }
        else if (input.startsWith(ENTITY_TYPE.toString()))
            return ENTITY_TYPE;
        else if (input.startsWith(TRUTH_TYPE.toString()))
            return TRUTH_TYPE;
        else if (input.startsWith(UNKNOWN_TYPE.toString()))
            return UNKNOWN_TYPE;
        else
            throw new Error("Unexpected type name" + input);
    }

    public static void main(String[] args) {
        String s1 = "<e,t>";
        String s2 = "?";
        try {
            Type t1 = parseType(s1);
            Type t2 = parseType(s2);
            System.out.println(t1.matches(t2));
        }
        catch (Error e){
            e.printStackTrace();
        }
    }
}
